#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //************************************* 对象初始化 ******************************************
    //连接、初始化手柄数据
    myGamepad = NULL;
    myPadValue = NULL;
    myMsg = NULL;
    myGamepad = new QGamepad(0, this);
    myPadValue = new GamepadValue();
    myMsg = new ControlMsgFrame(0, 0, 0, 0);
    //初始化套接字，建立通讯套接字（1个）
    tcpSocket = NULL;
    tcpSocket = new QTcpSocket(this);
    //初始化timer
    myTimer = new(QTimer);
    myTimer->start(100);  //每100ms发送一次消息帧


    //************************************ Tcp相关槽函数 ****************************************
    //显示连接信息
    connect(tcpSocket,&QTcpSocket::connected,[=]() {
        ui->textEditShow->setText("连接成功");
    });
    //实现通讯过程
    connect(tcpSocket,&QTcpSocket::readyRead,this,&MainWindow::on_tcpSocket_readyRead);

    //*********************************** 消息帧相关槽函数 ******************************************
    connect(myTimer, &QTimer::timeout,[=]() {
        if(ui->radioButtonGrounCont->isChecked() && tcpSocket->isValid() && myMsg->valide)
        {
            SendMsgFrame();
        }
    });  //定时发送手柄数据
    //*********************************** 手柄相关槽函数 ******************************************
    connect(myGamepad, &QGamepad::axisLeftXChanged, this, [=](double value){
       Gamepad_ChangeAxisValue(0, value);
    });  //更改摇杆左x值
    connect(myGamepad, &QGamepad::axisLeftYChanged, this, [=](double value){
       Gamepad_ChangeAxisValue(1, value);
    });  //更改摇杆左y值
    connect(myGamepad, &QGamepad::axisRightXChanged, this, [=](double value){
       Gamepad_ChangeAxisValue(2, value);
    });  //更改摇杆右x值
    connect(myGamepad, &QGamepad::axisRightYChanged, this, [=](double value){
       Gamepad_ChangeAxisValue(3, value);
    });  //更改摇杆右y值
    connect(myGamepad, &QGamepad::buttonL2Changed, this, [=](double value){
       Gamepad_ChangeAxisValue(4, value);
    });  //更改左扳机值
    connect(myGamepad, &QGamepad::buttonR2Changed, this, [=](double value){
       Gamepad_ChangeAxisValue(5, value);
    });  //更改右扳机值
    connect(myGamepad, &QGamepad::buttonL1Changed, this, [=](bool value){
       Gamepad_ChangeBoolValue(2, value);
    });  //更改左扳机值
    connect(myGamepad, &QGamepad::buttonR1Changed, this, [=](bool value){
       Gamepad_ChangeBoolValue(3, value);
    });  //更改右扳机值
}

MainWindow::~MainWindow()
{

    delete ui;
}

void MainWindow::Gamepad_ChangeAxisValue(int Chanel, double value)
{
    myPadValue->SetDoubleValue(Chanel, value);
    ProgressBar_Repaint();
}

void MainWindow::Gamepad_ChangeBoolValue(int Chanel, bool value)
{
    myPadValue->SetBoolValue(Chanel, value);
    ProgressBar_Repaint();
}

void MainWindow::ProgressBar_Repaint()
{
    int ch1 = 50 + 50 * myPadValue->leftX;
    int ch2 = 50 + 50 * myPadValue->leftY;
    int ch3 = 50 + 50 * myPadValue->rightX;
    int ch4 = 50 + 50 * myPadValue->rightY;
    int ch5 = int(myPadValue->left1) * 100;
    int ch6 = int(myPadValue->right1) * 100;
    int ch7 = myPadValue->left2 * 100;
    int ch8 = myPadValue->right2 * 100;



    ui->pgBarCh1->setValue(ch1);
    ui->pgBarCh2->setValue(ch2);
    ui->pgBarCh3->setValue(ch3);
    ui->pgBarCh4->setValue(ch4);
    ui->pgBarCh5->setValue(ch5);
    ui->pgBarCh6->setValue(ch6);
    ui->pgBarCh7->setValue(ch7);
    ui->pgBarCh8->setValue(ch8);

    if(ui->radioButtonGrounCont->isChecked() && tcpSocket->isValid())
    {
        myMsg->GenerateMsg(ch3, ch8, ch7, ch5); //生成消息帧
    }
}

void MainWindow::SendMsgFrame()
{
    tcpSocket->write(myMsg->MsgFrame.toUtf8().data());
    ui->textEditShow->append("Send: " + myMsg->MsgFrame);

}

void MainWindow::on_pushButtonConnect_clicked()
{
    //通过ui获取ip与端口，主动与服务器连接
    QString IP = ui->lineEditIP->text();
    quint16 Port = ui->lineEditPort->text().toUInt();
    //主动与服务器连接
    tcpSocket->connectToHost(QHostAddress(IP),Port);
    ui->statusbar->showMessage("Traget: " + IP + " : " + QString::number(Port));
}

void MainWindow::on_tcpSocket_readyRead()
{
    //获取对方发送的内容，显示到texteditRead中
    QByteArray array = tcpSocket->readAll();
    ui->textEditShow->append("Recive: " + array);  //追加显示
}


void MainWindow::on_pushButtonSend_clicked()
{
    //获取内容
    QString str = ui->textEditSend->toPlainText();
    //发送数据
    tcpSocket->write(str.toUtf8().data());
    //消息加入显示区
    ui->textEditShow->append("Send: " + str);
    //清空发送区
    ui->textEditSend->clear();
}

void MainWindow::on_textEditShow_textChanged()  //保持消息框的信息在最新
{
    ui->textEditShow->moveCursor(QTextCursor::End);
}
